package com.demo.sqlitedemo;

import android.Manifest;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SqliteActivity extends AppCompatActivity {

    SQLiteDatabase db;
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
        setContentView(R.layout.activity_main);

        list = findViewById(R.id.list);
        Button btnCreate = (Button) findViewById(R.id.btn_create);
        btnCreate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                db = SQLiteDatabase.openOrCreateDatabase(
                        "/mnt/sdcard/student.db", null);
                Toast.makeText(SqliteActivity.this, "连接数据库成功！", Toast.LENGTH_LONG).show();
                try {
                    Cursor cursor = db.rawQuery("select * from stu_info", null);
                } catch (SQLiteException e) {
                    e.printStackTrace();
                    db.execSQL("create table stu_info(_id integer primary key autoincrement, stu_id char(15),stu_name varchar(20))");
                    Toast.makeText(SqliteActivity.this, "创建数据表成功！", Toast.LENGTH_LONG).show();
                }
            }
        });

        Button btnInsert = findViewById(R.id.btn_insert);
        btnInsert.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ContentValues value = new ContentValues();
                value.put("stu_id", "20090001");
                value.put("stu_name", "zhang_san");
                db.insert("stu_info", null, value);

                value.put("stu_id", "20090002");
                value.put("stu_name", "li_si");
                db.insert("stu_info", null, value);

            }
        });

        Button btnQuerry = findViewById(R.id.btn_querry);
        btnQuerry.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Cursor cursor = db.rawQuery("select * from stu_info", null);
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(SqliteActivity.this, R.layout.line, cursor,
                        new String[]{"stu_id", "stu_name"}, new int[]{
                        R.id.stu_id, R.id.stu_name}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                // 显示数据
                list.setAdapter(adapter);
            }
        });

        Button btnUpdate = findViewById(R.id.btn_update);
        btnUpdate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ContentValues value = new ContentValues();
                // value.put("stu_id", "20090002");
                value.put("stu_name", "wang_wu");
                db.update("stu_info", value, "stu_id=?",
                        new String[]{"20090002"});
            }
        });

        Button btnDelete = findViewById(R.id.btn_delete);
        btnDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                db.delete("stu_info", "stu_id>?", new String[]{"20090001"});
            }
        });
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (db != null && db.isOpen()) {
            db.close();
        }
    }

}